package com.iwinding.ladygg.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.net.response.ThirdPartyLoginRp;

/**
 * 系统帮助类
 * Created by Administrator on 2016/9/20.
 */
public class SystemUtil {


//    public static LoginRp getICaneLoginRp(Context mContext){
//        final ICaneApplication iCaneApplication = ((ICaneApplication) mContext.getApplicationContext());
//        final LoginRp loginRp = iCaneApplication.getmLoginRp();
//        if(loginRp==null){
//            mContext.startActivity(new Intent(mContext, LoginActivity.class));
//            if(mContext instanceof MainActivity){
//                ((MainActivity) mContext).finish();
//            }
//        }
//        return loginRp;
//    }
    public static LoginRp getLoginRp(Context mContext){
        String loginRpJosn = SPUtils.getSpPropertyString(mContext, SPConstant.LOGIN_USER,"");
//        LogUtil.i("@hzy","loginRpJosn-----------"+loginRpJosn);
        LoginRp loginRp = GsonUtil.getInstance().formJson(loginRpJosn,LoginRp.class);
        return loginRp;
    }

    public static ThirdPartyLoginRp getThreeLoginRp(Context mContext){
        String loginRpJosn = SPUtils.getSpPropertyString(mContext, SPConstant.LOGIN_USER,"");
//        LogUtil.i("@hzy","thirdloginRpJosn-----------"+loginRpJosn);
        ThirdPartyLoginRp loginRp = GsonUtil.getInstance().formJson(loginRpJosn,ThirdPartyLoginRp.class);
        return loginRp;
    }
    /**
     * 退出应用
     *
     * @param mContext
     */
    public static void exitApp(Context mContext) {
//        MobclickAgent.onKillProcess(mContext);
//        ActivityManager activityMgr=(ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
//        activityMgr.restartPackage(mContext.getApplicationContext().getPackageName());
        System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 重启登录
     * @param context
     */
    public static void restartLogin(final Context context){
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    //版本名
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    //版本号
    public static String getVersionCode_Str(Context context) {
        return String.valueOf(getVersionCode(context));
    }

    public static String getPackageName(Context context) {
        return getPackageInfo(context).packageName;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

}
